package com.guiwu.gteway.industryapp.Device.View;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.guiwu.gteway.industryapp.Device.module.CameraInfo;
import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.util.MyLog;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants;
import com.videogo.realplay.RealPlayStatus;
import com.videogo.util.LogUtil;

/**
 * Created by qiaoguan on 2018/3/8. 监控点视频控制
 */

public class MonitorVideoFragment extends Fragment implements View.OnTouchListener{

    ImageButton ezPlayLeft, ezPlayTop ,ezPlayBottom ,ezPlayRight;
    LinearLayout mPtzControlLy;
    private static final String TAG = "MonitorVideoFragment";
    MonitorActivity activityParent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityParent = (MonitorActivity)getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.realplay_ptz_wnd,container,false);
        initView(view);
        return view;
    }

    private void initView(View view){
        ezPlayLeft = view.findViewById(R.id.ptz_left_btn);
        ezPlayLeft.setOnTouchListener(this);
        ezPlayBottom = view.findViewById(R.id.ptz_bottom_btn);
        ezPlayBottom.setOnTouchListener(this);
        ezPlayTop = view.findViewById(R.id.ptz_top_btn);
        ezPlayTop.setOnTouchListener(this);
        ezPlayRight = view.findViewById(R.id.ptz_right_btn);
        ezPlayRight.setOnTouchListener(this);
        mPtzControlLy = view.findViewById(R.id.ptz_control_ly);
    }

    public static MonitorVideoFragment newInstance() {
        Bundle args = new Bundle();
        MonitorVideoFragment fragment = new MonitorVideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                switch (v.getId()) {
                    case R.id.ptz_top_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_up_sel);
                     //   setPtzDirectionIv(RealPlayStatus.PTZ_UP);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTART);
                        break;
                    case R.id.ptz_bottom_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_bottom_sel);
                    //    setPtzDirectionIv(RealPlayStatus.PTZ_DOWN);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTART);
                        break;
                    case R.id.ptz_left_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_left_sel);
                   //     setPtzDirectionIv(RealPlayStatus.PTZ_LEFT);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTART);
                        break;
                    case R.id.ptz_right_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_right_sel);
                  //      setPtzDirectionIv(RealPlayStatus.PTZ_RIGHT);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTART);
                        break;
                    default:
                        break;
                }
                break;
            case MotionEvent.ACTION_UP:
                switch (v.getId()) {
//                    case R.id.talkback_control_btn:
//                        mEZPlayer.setVoiceTalkStatus(false);
//                        mTalkRingView.setVisibility(View.GONE);
//                        break;
                    case R.id.ptz_top_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_bg);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTOP);
                        break;
                    case R.id.ptz_bottom_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_bg);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTOP);
                        break;
                    case R.id.ptz_left_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_bg);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTOP);
                        break;
                    case R.id.ptz_right_btn:
                        mPtzControlLy.setBackgroundResource(R.mipmap.ptz_bg);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTOP);
                        break;
                    default:
                        break;
                }
        }
        return false;
    }


    /**
     * 云台操作
     *
     * @param command ptz控制命令
     * @param action  控制启动/停止
     */
    private void ptzOption(final EZConstants.EZPTZCommand command, final EZConstants.EZPTZAction action) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean ptz_result = false;
                try {
                    CameraInfo info = activityParent.getCurrentCameraInfo();
                    ptz_result = activityParent.getEzOpenSDK().controlPTZ(info.getDeviceSerial(), info.getChannelNo(), command,
                            action, EZConstants.PTZ_SPEED_DEFAULT);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                MyLog.i(TAG, "controlPTZ ptzCtrl result: " + ptz_result);
            }
        }).start();
    }
}
